<template>
  <div class="app">
    <h1>{{ msg }},学生姓名是：{{ studentName }}</h1>
    <School :getSchoolName="getSchoolName"></School>

    <!-- 第一种写法，使用@或v-on -->
    <Student @atrent="getStudentName" @demo="m1"></Student>
    <!-- 第二种写法，使用ref -->
    <!-- native：组件使用原生事件 -->
    <Student ref="student1" @click.native="show"/>
  </div>
</template>

<script>
  import School from './components/School.vue';
  import Student from './components/Student.vue';

  export default {
    name:'App',
    components:{School,Student},
    data(){
      return {
        msg:'hello!',
        studentName:''
      }
    },
    methods:{
      getSchoolName(name){
        console.log('App收到学校名:',name);
      },
      getStudentName(name){
        console.log('App收到学生名',name);
        this.studentName = name;
      },
      m1(){
        console.log('demo触发');
      },
      show(){
        alert('clicked');
      }
    },
    mounted(){
      this.$refs.student1.$on('atrent',this.getStudentName)
      // 只触发一次
      //this.$refs.student.$once('atrent',this.getStudentName)
    }
  }
</script>

<style>
 .app{
  background-color: grey;
  padding: 5px;
 }
</style>